﻿// Config_DruidDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "Clicker.h"
#include "Config_DruidDlg.h"
#include "afxdialogex.h"
#include "DefInfo/GameInfo.h"
#include "Resource.h"
#include "boost/format.hpp"


// Config_DruidDlg 对话框

IMPLEMENT_DYNAMIC(Config_DruidDlg, CDialogEx)

Config_DruidDlg::Config_DruidDlg(SpecializationID spec_id_value, CString title_value, CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CONFIG_DRUID, pParent), spec_id(spec_id_value), title(title_value)
{

}

Config_DruidDlg::~Config_DruidDlg()
{
}

void Config_DruidDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(Config_DruidDlg, CDialogEx)
	ON_WM_HSCROLL()
END_MESSAGE_MAP()


// Config_DruidDlg 消息处理程序


BOOL Config_DruidDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	this->SetWindowText(title);
	InitCtrl();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void Config_DruidDlg::InitCtrl()
{
	auto& vec = GameInfo::inst().GetRoutineConfig().ClassConfig[spec_id]->vec_property;

	int index = 0;

	//IDC_STATIC_TEMPLATE

	//HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
	CWnd* copy_ctrl = this->GetDlgItem(IDC_STATIC_TEMPLATE);
	for (auto& prop : vec)
	{
		RowCtrls row(prop);
		auto label = CreateTitle(prop, index, copy_ctrl);
		row.vec_ctrl.push_back(label);
		if (prop.condition == PC_if_hp_percent_less_then_value)
		{
			auto slider = CreateSlide(prop, index);
			row.vec_ctrl.push_back(slider);
			auto editor = CreateEditor(prop, index);
			row.vec_ctrl.push_back(editor);
		}
		rows_ctrls.push_back(row);
		index++;
	}
}

std::shared_ptr<CStatic> Config_DruidDlg::CreateTitle(Property &prop, int index, CWnd* copy_ctrl)
{
	std::shared_ptr<CStatic> label = std::make_shared<CStatic>();
	ASSERT_VALID(label.get());
	CA2T name(prop.name.c_str());
	CString name_format;
	name_format.Format(_T("%s:"), name.m_psz);
	CRect rect(10, 10 + index * 25, 120, 10 + index * 25 + 23);
	//CRect rect(50, 80, 150, 150);
	label->Create(name, WS_CHILD | WS_VISIBLE | SS_LEFT | SS_CENTERIMAGE | WS_BORDER, rect, this);
	label->SetWindowText(name_format);
	if (copy_ctrl)
	{
		auto font = copy_ctrl->GetFont();
		label->SetFont(font);
	}
	return label;
}

std::shared_ptr<CSliderCtrl> Config_DruidDlg::CreateSlide(Property& prop, int index)
{
	std::shared_ptr<CSliderCtrl> slider = std::make_shared<CSliderCtrl>();
	ASSERT_VALID(slider.get());
	CRect rect(122, 10 + index * 25, 280, 10 + index * 25 + 23);
	auto create_id = slider_id_range.GetAvaildID();
	slider->Create(WS_CHILD | WS_VISIBLE | WS_BORDER, rect, this, create_id);
	slider->SetRange(0, 1000);
	slider->SetLineSize(1);
	int pos = static_cast<int>(std::any_cast<double>(prop.value) * 1000);
	slider->SetPos(pos);
	slider_ref_id[create_id] = index;
	return slider;
}

std::shared_ptr<CEdit> Config_DruidDlg::CreateEditor(Property& prop, int index)
{
	std::shared_ptr<CEdit> editor = std::make_shared<CEdit>();
	ASSERT_VALID(editor.get());
	CRect rect(282, 10 + index * 25, 380, 10 + index * 25 + 23);
	editor->Create(WS_CHILD | WS_VISIBLE | WS_BORDER, rect, this, editor_id_range.GetAvaildID());
	SetEditorContent(prop, *editor);
	return editor;
}

void Config_DruidDlg::SetEditorContent(Property &prop, CEdit& editor)
{
	try
	{
		double value = std::any_cast<double>(prop.value) * 100.0f;
		CString strValue;
		strValue.Format(L"%.2f", value);
		editor.SetWindowText(strValue);
	}
	catch (const std::bad_any_cast& e) {
		OutputDebugStringA(str(boost::format("%s\n") % e.what()).c_str());
	}
}


void Config_DruidDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	auto ctrl_id = pScrollBar->GetDlgCtrlID();
	auto row_ctrl = GetRowCtrlsBySliderID(ctrl_id);
	CSliderCtrl* slider = (CSliderCtrl*)pScrollBar;
	int curr_pos = slider->GetPos();
	double curr_value = curr_pos / 1000.0f;
	row_ctrl.ref_prop.value = curr_value;
	DbTool::inst().UpdateProperty(row_ctrl.ref_prop);
	CEdit* edit_point = static_cast<CEdit*>(row_ctrl.vec_ctrl[2].get());
	SetEditorContent(row_ctrl.ref_prop, *edit_point);
	CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}

Property &Config_DruidDlg::GetSliderProperty(UINT32 slider_id)
{
	auto it = slider_ref_id.find(slider_id);
	if (it != slider_ref_id.end())
	{
		auto index = it->second;
		return rows_ctrls[index].ref_prop;
	}
	throw std::exception("out of range");
}

Config_DruidDlg::RowCtrls &Config_DruidDlg::GetRowCtrlsBySliderID(UINT32 slider_id)
{
	auto it = slider_ref_id.find(slider_id);
	if (it != slider_ref_id.end())
	{
		auto index = it->second;
		return rows_ctrls[index];
	}
	throw std::exception("out of range");
}
